package com.tiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiku.common.ResultJson;
import com.tiku.controller.DTO.ActivationDTO;
import com.tiku.entity.Activation;
import com.tiku.entity.ProductSpecifications;
import com.tiku.entity.TypeClass;
import com.tiku.entity.UserPayTiku;
import com.tiku.service.*;
import com.tiku.mapper.ActivationMapper;
import com.tiku.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class ActivationServiceImpl extends ServiceImpl<ActivationMapper, Activation>
    implements ActivationService{


    @Autowired
    private ActivationMapper activationMapper;

    @Autowired
    private SingleSubjectQuestionBanksService  questionBanksService;

    @Autowired
    private TypeClassService typeClassService;

    @Autowired
    private ProductSpecificationsService specificationsService;

    @Autowired
    private UserPayTikuService userPayTikuService;
    @Override
    public ResultJson getActivation(String code) {
        if(StringUtils.isEmpty(code)||StringUtils.isEmpty(code.trim())){
            return ResultJson.error("激活码不能为空");
        }
        Activation activation = activationMapper.selectOne(new LambdaQueryWrapper<Activation>().eq(Activation::getActivationCode, code));
        if(Objects.isNull(activation)){
            return ResultJson.error("激活码不存在");
        }
        Integer status = activation.getStatus();
        if (Objects.equals(status, 1)){
            return ResultJson.error("该激活码已被使用");
        }

        String ssqbId = activation.getSsqbId();
        ActivationDTO activationDTO=new ActivationDTO();
        activationDTO.setSetmealId(ssqbId);
        //获取题库的题目
        String name = questionBanksService.getById(ssqbId).getName();
        activationDTO.setSetmealName(name);

        //获取题库对应的科目类型集合

        List<TypeClass> list = typeClassService.list(new LambdaQueryWrapper<TypeClass>().eq(TypeClass::getSetMealId, ssqbId).select(TypeClass::getTypeId,TypeClass::getTypeName));
        activationDTO.setList(list);



        return ResultJson.success(activationDTO);
    }

    @Override
    public ResultJson getKemu(String id) {
        List<ProductSpecifications> list = specificationsService.list(new LambdaQueryWrapper<ProductSpecifications>()
                .eq(ProductSpecifications::getTypeClassId, id));

        return ResultJson.success(list);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultJson activated(String activateCode, String specId) {
        //校验激活码 与 课程
        Activation activation = activationMapper.selectOne(new LambdaQueryWrapper<Activation>()
                .eq(Activation::getActivationCode, activateCode));
        ProductSpecifications specifInfo = specificationsService.getById(specId);
        ResultJson resultJson = checkActiva(activation, specifInfo);
        if (Objects.nonNull(resultJson)){
            return resultJson;
        }
        //校验合格 添加用户权限
        String userId = ThreadLocalUtil.getThreadLocal();
        activation.setStatus(1);
        activation.setUseTime(new Date());
        activationMapper.updateById(activation);

        UserPayTiku userPayTiku=new UserPayTiku();
        userPayTiku.setUserId(userId);
        userPayTiku.setPaySpecId(specId);
        userPayTiku.setCreateTime(new Date());

        boolean save = userPayTikuService.save(userPayTiku);
        return save?ResultJson.success():ResultJson.sysError();
    }

    private ResultJson checkActiva(Activation activation, ProductSpecifications specifInfo) {

        if (Objects.isNull(activation)){
            return ResultJson.error("激活码无效!");
        }
        Integer status = activation.getStatus();
        if (Objects.equals(status, 1)){
            return ResultJson.error("该激活码已被使用!");
        }
        if (Objects.isNull(specifInfo)){
            return ResultJson.error("该课程规格不存在!");
        }

        String setMealId = specifInfo.getSetMealId();

        if (!activation.getSsqbId().equals(setMealId)){
            return ResultJson.error("课程规格与激活码不对应!");
        }
        UserPayTiku one = userPayTikuService.getOne(new LambdaQueryWrapper<UserPayTiku>()
                .eq(UserPayTiku::getUserId, ThreadLocalUtil.getThreadLocal())
                .eq(UserPayTiku::getPaySpecId, specifInfo.getSpecid()));
        if (Objects.nonNull(one)){
            return ResultJson.error("该课程规格已激活！");
        }

        return null;
    }
}




